package com.tanhua.server.service;

import com.tanhua.autoconfig.template.CosTemplate;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.domain.UserInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;

@Service
public class UserInfoService {

    @DubboReference
    private UserInfoApi userInfoApi;

    @Autowired
    private CosTemplate cosTemplate;


    /**
     * 保存用户信息
     *
     * @param userInfo
     */
    public void save(UserInfo userInfo) {
        userInfoApi.save(userInfo);
    }


    /**
     * 更新头像
     *
     * @param headPhoto
     * @param id
     */
    public void updateHead(File headPhoto, Long id) {
        String imageUrl = cosTemplate.upload(headPhoto, headPhoto.getName());
        // 本来要判断是否包含人脸，没有该API，逻辑如下。包含人脸则继续逻辑，不包含则抛出异常
        UserInfo userInfo = new UserInfo();
        userInfo.setId(id);
        userInfo.setAvatar(imageUrl);
        userInfoApi.update(userInfo);
    }

    public UserInfo findById(Long userId) {
        return userInfoApi.findById(userId);
    }

    /**
     * 更细用户信息
     *
     * @param userInfo
     */
    public void update(UserInfo userInfo) {
        userInfoApi.update(userInfo);
    }
}
